<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 优惠券接口
 */
class Coupon extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 获取用户优惠券
     *
     */
    public function index()
    {   
        $data = [];
        $page = $this->request->request('page',1);//分页
        $user_id = $this->request->param('user_id');//用户ID
        $pageNum  = 6;//每页个数

        if(empty($user_id)){
            $this->error('用户信息错误');
        }

        $where = array();
        $where['user_id'] = $user_id;

        $start = ($page-1)*$pageNum;
        //查询总页数
        $count = db('coupon_list')->where($where)->where(array('status' => array('NEQ', 3)))->count();
        $maxpages = ceil($count/$pageNum);
        if($page > $maxpages){
            $this->success('最后一页了',$data);
        }

        //查询是否可用优惠券
        $list = db('coupon_list')->where($where)->where(array('status' => array('NEQ', 3)))->limit($start,$pageNum)->select();
        if(!empty($list)){
            foreach($list as $k=>$v){

                    //状态
                    if($v['status'] == 0){
                        $list[$k]['status_title'] = "未使用";
                    }else if($v['status'] == 1){
                        $list[$k]['status_title'] = "已使用";
                    }else if($v['status'] == 2){
                        $list[$k]['status_title'] = "已过期";
                    }else if($v['status'] == 3){
                        $list[$k]['status_title'] = "已删除";
                    }

                    //使用者
                    $list[$k]['user_info'] = "-";
                    //订单信息
                    $list[$k]['order_info'] = "-";
                    //优惠内容
                    $list[$k]['coupon_info'] = "抵价".$v['money']."元";

                    //批次名称
                    $row = model('\app\admin\model\Coupon')->get($v['coupon_id'])->toArray();
                    $list[$k]['title_info'] = $row['title'];

                    //领取时间
                    $list[$k]['start_time'] = empty($v['start_time']) ? "-" : date("Y-m-d H:i:s",$v['start_time']);
                    $list[$k]['end_time'] = empty($v['end_time']) ? "-" : date("Y-m-d H:i:s",$v['end_time']);
                    //使用时间
                    $list[$k]['use_time'] = empty($v['use_time']) ? "-" : date("Y-m-d H:i:s",$v['use_time']);
            }
        }

        $this->success('查询成功',$list);
    }

    /**
     * 验证优惠券是否合法接口
     *
     */
    public function verifycode()
    {
        $coupon_id = $this->request->param('coupon_id');//优惠券ID
        $user_id = $this->request->param('user_id');//用户ID

        if(empty($coupon_id) || empty($user_id)){
            $this->error('查询信息错误');
        }

        //查询是否可用优惠券
        $coupon = db('coupon_list')->where('id',$coupon_id)->where('user_id',$user_id)->find();
        if(!empty($coupon['id'])){
            
            //状态
            if($coupon['status'] == 0){

                $new_time = time();
                if(($coupon['start_time'] <= $new_time) &&  ($new_time <= $coupon['end_time'])){
                    $this->success('此优惠券合法', $coupon);
                }else{

                    //修改信息
                    db('coupon_list')->where('id', $coupon_id)->update(['status' => 2]);
                    $this->error('此优惠券已过期');
                }

            }else if($coupon['status'] == 1){
                $this->error('此优惠券已被使用');
            }else if($coupon['status'] == 2){
                $this->error('此优惠券已过期');
            }else if($coupon['status'] == 3){
                $this->error('此优惠券已删除');
            }

        }else{
            $this->error('未查到此优惠券信息');
        }

    }

}
